import Vue from 'vue'
import App from './App.vue'
import router from './router'
import './plugins/element.js'
import './assets/css/common.css'
import axios from 'axios'

// 导入阿里图标素材
import './assets/fonts/iconfont.css'
// 图表控件
import './plugins/wyz-echarts/wyz-echarts.js'

Vue.config.productionTip = false
const baseUrl = 'http://www.localhost:8888/api/private/v1'

Vue.prototype.$upLoad = function ({ url, params }) {
  var loading = this.$loading({
    lock: true,
    text: '正在加载中，请稍后...',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)'
  })
  const headers = []
  // 添加一些整体的请求头字段，当然也可以使用请求拦截器来整体添加
  headers['Content-type'] = 'multipart/form-data'
  headers.Authorization = window.sessionStorage.getItem('token')
  var instance = axios.create({
    baseURL: baseUrl,
    timeout: 10000,
    headers
  })
  const method = 'POST'
  const isGet = ((method == null) || (method.toString().toUpperCase() === 'GET') || (method.toString().toUpperCase() === 'DELETE'))
  return instance.request({
    method: method == null ? 'GET' : method,
    url: url,
    data: isGet ? {} : params,
    params: isGet ? params : {}
  }).then((res) => {
    loading.close()
    const data = res.data
    if (data.meta.status !== 200) {
      this.$message.error({
        showClose: true,
        message: `${data.meta.msg}`,
        duration: 1500
      })
    }
    return data
  }, (reason) => {
    loading.close()
    return reason
  })
}
Vue.prototype.$send = async function ({ url, params, method, headers, fullscreen }) {
  if (fullscreen) {
    var loading = this.$loading({
      lock: true,
      text: '正在加载中，请稍后...',
      spinner: 'el-icon-loading',
      background: 'rgba(0, 0, 0, 0.7)'
    })
  }
  headers = headers == null ? {} : headers
  // 添加一些整体的请求头字段，当然也可以使用请求拦截器来整体添加
  headers['Content-type'] = 'application/json'
  headers.Authorization = window.sessionStorage.getItem('token')
  var instance = axios.create({
    baseURL: baseUrl,
    timeout: 6000,
    headers
  })
  // 这种是以async  await 这种方式来写，可以直接获取请求函数
  // 我们这边需要加上element-ui的全局弹窗就不用
  /* const { data } = await instance.request({
    method: method == null ? 'GET' : method,
    url: url,
    data: params
  })
  return data */
  const isGet = ((method == null) || (method.toString().toUpperCase() === 'GET') || (method.toString().toUpperCase() === 'DELETE'))
  return instance.request({
    method: method == null ? 'GET' : method,
    url: url,
    data: isGet ? {} : params,
    params: isGet ? params : {}
  }).then((res) => {
    if (fullscreen) {
      loading.close()
    }
    const data = res.data
    if (data.meta.status !== 200) {
      this.$message.error({
        showClose: true,
        message: `${data.meta.msg}`,
        duration: 1500
      })
    }
    return data
  }, (reason) => {
    if (fullscreen) {
      loading.close()
    }
    return reason
  })
}
Vue.prototype.$showMsg = function(msg) {
  this.$message.success({
    showClose: true,
    message: msg,
    duration: 1500
  })
}

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
